//
//  securityVerificationVC.swift
//  甜言蜜语
//
//  Created by yingli on 16/7/22.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit
import SVProgressHUD
class securityVerificationVC: UIViewController {
    var id : Int = 0
    var type: String = ""
    var url: String = ""
    var SecureUrl: String = ""//安全验证的验证地址
    var sendUrl: String = ""//发送手机验证的地址
    @IBOutlet weak var phone: UITextField!
    @IBOutlet weak var idenCode: UITextField!
    @IBOutlet weak var phoneCode: UITextField!
    @IBOutlet weak var smsBtn: UIButton!
    @IBOutlet weak var iv: UIImageView!
    var timeTick: Int = 0
    var timer: NSTimer?
    var msg: regiestInfo?{
        didSet{
            
        }
    }
    var modelList: checkSecureAddressInfo?{
        didSet{
            self.iv.sd_setImageWithURL(NSURL(string:(self.modelList?.code)!))
            
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        publicMethod.setHUDStyle()
        if self.type == "活动"{
            self.SecureUrl = String(format: "%@activity/dosel/verifycode",BaseUrl)
            
        }
        if self.type == "积分"{
            self.SecureUrl = String(format: "%@mall/dosel/verifycode",BaseUrl)
            
        }
        DataTool.secureCheckAddress(self.SecureUrl,completionHandler: { (newModel) in
            guard let newDataes = newModel else {
                return
            }
            
            print(newDataes)
            self.modelList = newDataes
            
        })
        
    }
    
    override func viewWillDisappear(animated: Bool) {
        SVProgressHUD.dismiss()
        super.viewWillDisappear(animated)
    }
    override func viewWillAppear(animated: Bool) {
        self.timer?.fireDate = NSDate.distantPast()
        super.viewWillAppear(animated)
    }
    
    override func viewDidDisappear(animated: Bool) {
        self.timer?.fireDate = NSDate.distantFuture()
        super.viewDidDisappear(animated)
    }
    
    func sendIdfentiCode() {
        self.smsBtn.backgroundColor = UIColor.lightGrayColor()
        timeTick = 61
        //60秒倒计时
        self.timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(self.timeFireMethod), userInfo: nil, repeats: true)
        self.smsBtn.enabled = false
    }
    
    
    func timeFireMethod() {
        timeTick -= 1
        if timeTick == 0 {
            timer!.invalidate()
            //停止
            //self.timer = 0
            self.smsBtn.enabled = true
            self.smsBtn.setTitle("重新获取", forState: .Normal)
            self.smsBtn.backgroundColor = NAVIGATIONBAR_RED_COLOR
        }
        else {
            let str: String = "\(timeTick)"
            self.smsBtn.setTitle("\(str)秒", forState: .Normal)
        }
    }
    
    //MARK:sendIdentyCode
    @IBAction func sendIdentyCode(sender: UIButton){
        if self.idenCode.text == ""{
            SVProgressHUD.showErrorWithStatus("请输入验证码")
            
            return
        }else{
            if self.type == "活动"{
                self.sendUrl = String(format: "%@activity/dosel/activitycode",BaseUrl)
            }
            if self.type == "积分"{
                self.sendUrl = String(format: "%@mall/dosel/mallcode",BaseUrl)
            }
            
            DataTool.sendPhoneIdentyCode(self.idenCode.text!,url: self.sendUrl, completionHandler:{ (newModel) in
                guard let newDataes = newModel else {
                    return
                }
                print(newDataes)
                self.msg = newDataes
                if self.msg?.msg == "发送成功"{
                    self.sendIdfentiCode()
                    SVProgressHUD.showSuccessWithStatus(self.msg!.msg)
                    
                }else{
                    
                    SVProgressHUD.showErrorWithStatus(self.msg!.msg)
                }
            })

        }
    }
    
    //MARK:nextCheck
    @IBAction func checkNext(sender: UIButton){
        if self.phoneCode.text == ""{
            SVProgressHUD.showErrorWithStatus("请输入手机验证码")
            return
        }
        if self.type == "活动"{
            self.url = String(format: "%@activity/dosel/next", BaseUrl)
            
        }
        if self.type == "积分"{
            self.url = String(format: "%@mall/dosel/next", BaseUrl)
            
        }
        DataTool.checkNext(self.idenCode.text!, code: self.phoneCode.text!,url:self.url, completionHandler: { (newModel) in
            guard let newDataes = newModel else {
                return
            }
            print(newDataes)
            self.msg = newDataes
            SVProgressHUD.showSuccessWithStatus(self.msg?.msg)
        
        let vc = self.storyboard?.instantiateViewControllerWithIdentifier("harvestAddressVC") as! harvestAddressVC
        vc.id = self.id
        vc.type = self.type
        self.navigationController?.pushViewController(vc, animated: true)
        })

    }

}
